<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a>结算</a>
        <a><cite>审核</cite></a>
    </div>
</div>
<style>
    table{
        text-align: center;
    }
    .leftT {
        text-align: left;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">审核</li>
            </ul>
            <div class="layui-tab-content">
                <table id="audit" class="layui-table"></table>
                <form class="layui-form">
                    <div class="layui-form-item" id="pollWeightDiv">
                        <label class="layui-form-label">金额分配</label>
                        <div class="layui-input-block" >
                            <table id="Passage_dataAll" lay-filter="Passage_dataAll"></table>
                        </div>
                    </div>
                    <div class="layui-form-item" >
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <input type="text" name="remark" id="remTxt" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block" >
                            <button id="auditTg" lay-submit lay-filter="auditTg" class="layui-btn layui-btn-normal" data-val="2">审核通过</button>
                            <button id="auditFai" lay-submit lay-filter="auditFai" class="layui-btn layui-btn-danger" data-val="3">审核不通过</button>
                            <a lay-href="sett/audit/" class="layui-btn">返回</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
        
    layui.use(['admin', 'table','util','element', 'form'], function(){
        var table = layui.table
        ,$ = layui.$
        ,form = layui.form
        ,table = layui.table
        ,admin = layui.admin
        ,element = layui.element;
        
        element.render('breadcrumb', 'breadcrumb');//渲染导航信息
        var router = layui.router();
        var creId = router.search.id;

        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/sett/get',
            data: {
                id: creId,
            },
            error: function(err){
                layer.alert(err);
            },
            success: function(res){
                var infoId = res.data.infoId;
                var infoType = res.data.infoType;
                if(res.code == 0 && res.data != null){
                    var html = '';
                    html += `
                        <table>
                            <colgroup>
                                <col width="25%">
                                <col>
                                <col width="25%">
                                <col>
                            </colgroup>
                            <tbody>
                                <tr>
                                    <td>结算订单号</td>
                                    <td class="leftT">${res.data.settOrderId == null ? "" : res.data.settOrderId}</td>
                                    <td>结算日期</td>
                                    <td class="leftT">${ layui.util.toDateString(res.data.settDate, "yyyy-MM-dd") }</td>
                                </tr>
                                <tr>
                                    <td>结算商类型</td>
                                    <td class="leftT">${res.data.infoType==1?"代理商":res.data.infoType==2?"商户":""}</td>
                                    <td>结算商</td>
                                    <td class="leftT">${res.data.infoName}[${res.data.infoId}]</td>
                                </tr>
                                <tr>
                                    <td>结算类型</td>
                                    <td class="leftT">${res.data.settType==1?"手工结算":res.data.settType==2?"自动结算":""}</td>
                                     <td>结算金额</td>
                                    <td class="leftT" id="settAmount">${res.data.settAmount/100 }</td>
                                </tr>
                                <tr>
                                    <td>打款金额</td>
                                    <td class="leftT">${res.data.remitAmount/100}</td>
                                    <td>手续费</td>
                                    <td class="leftT">${res.data.settFee/100}</td>
                                </tr>
                                <tr>
                                    <td>账户属性</td>
                                    <td class="leftT">${res.data.accountAttr==0?"对私":res.data.accountAttr==1?"对公":""}</td>
                                    <td>账户类型</td>
                                    <td class="leftT">${res.data.accountType==1?"银行账户":res.data.accountType==2?"微信账户":res.data.accountType==3?"支付账户":""}</td>
                                </tr>
                                <tr>
                                    <td>开户行名称</td>
                                    <td class="leftT">${res.data.bankName}</td>
                                    <td>开户网点名称</td>
                                    <td class="leftT">${res.data.bankNetName}</td>
                                </tr>
                                <tr>
                                    <td>账户名</td>
                                    <td class="leftT">${res.data.accountName}</td>
                                    <td>账号</td>
                                    <td class="leftT">${res.data.accountNo}</td>
                                </tr>
                                <tr>
                                    <td>开户行所在省</td>
                                    <td class="leftT">${res.data.province==null?"":res.data.province}</td>
                                    <td>开户行所在市</td>
                                    <td class="leftT">${res.data.city==null?"":res.data.city}</td>
                                </tr>
                                <tr>
                                    <td>结算状态</td>
                                    <td class="leftT">${res.data.settStatus == 1?"等待审核":res.data.settStatus == 2?"已审核":res.data.settStatus == 3?"审核不通过":res.data.settStatus == 4?"打款中":res.data.settStatus == 5?"打款成功":res.data.settStatus == 6?"打款失败":"非法结算状态"}</td>
                                    <td>创建时间</td>
                                    <td class="leftT">${ layui.util.toDateString(res.data.createTime) }</td>
                                </tr>
                                <tr>
                                    <td>操作人ID</td>
                                    <td class="leftT">${res.data.operatorId == null ? "" : res.data.operatorId}</td>
                                    <td>备注</td>
                                    <td class="leftT">${res.data.remark}</td>
                                </tr>
                            </tbody>
                        </table>
                    `
                    $('#audit').html(html);

                    var settAmount = $("#settAmount").text();
                    // 通道轮询
                    table.render({
                        elem: '#Passage_dataAll'
                        ,url: layui.setter.baseUrl + '/sett/getPassage'
                        ,where: {
                            access_token: layui.data(layui.setter.tableName).access_token,
                            infoId: infoId,
                            infoType: infoType,
                            settAmount: settAmount
                        }
                        ,id: 'tablePassage'
                        ,page: false
                        ,cols: [[
                            {field: 'ok', type: 'checkbox', fixed: 'left', width: 80, LAY_CHECKED: true}
                            ,{field: 'id', title: '通道ID', width: 100}
                            ,{field: 'name', title: '通道名称'}
                            ,{field: 'settAmount', edit: 'text', title: '结算金额(元)', width: 150}
                            ,{field: 'settFee', title: '结算手续费', width: 150}
                            ,{field: 'canSettAmount', title: '可用余额', width: 150}
                            ,{field: 'balance', title: '余额', width: 150}
                        ]]
                        ,skin: 'line'
                        ,done: function(res, curr, count){
                        }
                    });
                    form.render();
                }else {
                    layer.alert('请求数据为空');
                }
            }
        });

        //当点击审核通过按钮时
        form.on('submit(auditTg)', function(data){
            var settAmount = $("#settAmount").text();
            // 获取表格选中行的数据
            var checkDatas = table.checkStatus('tablePassage');
            var settPassage = [];
            var passageAmount = 0;
            $.each(checkDatas.data, function(index, val) {
                var p = {};
                p.id = val.id;
                if (val.settAmount) {
                    p.settAmount = val.settAmount;
                } else {
                    p.settAmount = 0;
                }
                if (val.settFee) {
                    p.settFee = val.settFee;
                } else {
                    p.settFee = 0;
                }
                passageAmount += parseInt(val.settAmount);
                settPassage.push(p);
            });
            if(passageAmount > settAmount){
                layer.alert('通道结算金额之和不能大于申请结算金额: ' + settAmount);
                return false;
            }
            // 在这个回调函数里面写ajax提交
            var curSt = $('#auditTg').data('val');
            admin.req({
                type: 'post',
                url: layui.setter.baseUrl + '/sett/audit',
                data: {
                    settPassage: JSON.stringify(settPassage),
                    remark: data.field.remark,
                    status: curSt,
                    id:creId
                },
                error: function(err){
                    layer.alert(err,{title:'发起的请求失败'})
                },
                success: function(res){
                    if(res.code == 0){
                        layer.alert(res.msg ,function(index){
                            layer.close(index);
                            location.hash = '/sett/audit/';
                        })
                    }
                }
            })
            return false;//阻止跳转
        });

        form.on('submit(auditFai)', function(data){
            // 在这个回调函数里面写ajax提交
            var curSt = $('#auditFai').data('val');
            admin.req({
                type: 'post',
                url: layui.setter.baseUrl + '/sett/audit',
                // data: "params=" + JSON.stringify(data.field),
                data: {
                    remark : data.field.remark,
                    status: curSt,
                    id:creId
                },
                error: function(err){
                    layer.alert(err,{title:'发起的请求失败'})
                },
                success: function(res){
                    if(res.code == 0){
                        layer.alert(res.msg ,function(index){
                            layer.close(index);
                            location.hash = '/sett/audit/';
                        })
                    }
                }
            })
        });
        form.render();
     
    })

</script>